package com.cn.zengzeng.spbzengzeng.config;

import com.cn.zengzeng.spbzengzeng.interceptor.JwtAuthenIntercetpor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置自定义拦截器
 * @Author: zhangzengzeng
 * @Date: 2021/6/23 15:54
 */
@Configuration
public class IntercetorConfig implements WebMvcConfigurer {
    private static final long SECOND =3600L;
    @Bean
    public JwtAuthenIntercetpor jwtAuthenIntercetpor(){
        return new JwtAuthenIntercetpor();
    }
    @Override
    public void addInterceptors (InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        //首页
            excludePath.add("/login");
        //重试激活账号
        excludePath.add("/user/tryActiveAccount/");
        //登出
        excludePath.add("/logout");
        //注册
        excludePath.add("/register");
        //静态资源
        excludePath.add("/static/**");
        //静态资源
        excludePath.add("/assets/**");
        excludePath.add("classpath:/static/");
//        registry.addInterceptor(logInterceptor());
        registry.addInterceptor(jwtAuthenIntercetpor())
                .addPathPatterns("/**")
                //排除拦截路径
                .excludePathPatterns(excludePath);
    }
    //通用来出各种跨域的问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry
                //                允许跨域的路径
                .addMapping("/**")
                //                允许跨域的path
                .allowedOrigins("*")
                //                允许凭证
                .allowCredentials(true)
                //                允许通过方法
                .allowedMethods("GET","POST","DELETE","PUT","PATCH","OPTIONS","HEAD")
                //               跨域的生存期
                .maxAge(SECOND*24L);
    }

/*    @Bean
    public LogInterceptor logInterceptor() {
        return new LogInterceptor();
    }*/
}
